package com.barunsw.bwf.ems.neoarena.ne.tamms.mgcp;


public class MgcpControl implements MGCPConstants {
	final int   ERROR_LENGTH = 128;
	java.io.DataInputStream stream = null;

	public MgcpControl(char[] data) {
		try {
			byte[] newData = new byte[data.length];
			for ( int i = 0; i < data.length; i++ )
				newData[i] = (byte)data[i];

			stream = new java.io.DataInputStream(new java.io.ByteArrayInputStream(newData));

		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	public M2600_MODE getDisRtpCap(){
		M2600_MODE mode = new M2600_MODE();
		byte[] name = new byte[16];
		try{
			stream.read(name, 0, name.length);
			mode.send_ipaddr = (new String(name)).trim();
			mode.send_port = stream.readInt();
			mode.Ln = stream.readInt();
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}

	public M3018_MODE getDisDspUse(){
		M3018_MODE mode = new M3018_MODE();
		try{
			for(int i=0; i<mode.VocDB.length; i++){
				mode.VocDB[i] = new M_Voc_Info();
				mode.VocDB[i].VocAvlCnt = stream.readShort();
				mode.VocDB[i].VocAlocCnt = stream.readShort();
				mode.VocDB[i].LoadFlag = stream.readShort();
				
				stream.readShort();

				for(int j=0; j<20; j++)
					mode.VocDB[i].VocSts[j] = stream.readInt();
				for(int j=0; j<20; j++)
					mode.VocDB[i].VocUse[j] = stream.readInt();
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}


	public M3113_MODE getDisDataMedia(){
		M3113_MODE mode = new M3113_MODE();
		try{
			mode.dev_type = stream.readInt();
			mode.vad_cng_flag = stream.readInt();
			mode.echo_flag = stream.readInt();
			mode.echo_nlp = stream.readInt();
			mode.echo_cn_mode = stream.readInt();
			mode.echo_tail_len = stream.readInt();
			mode.jitter_type = stream.readInt();
			mode.jitter_buf_init_size = stream.readInt();
			mode.jitter_buf_min_size = stream.readInt();
			mode.jitter_buf_max_size = stream.readInt();
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}

	public M3502_MODE getDisMgc(){
		M3502_MODE mode = new M3502_MODE();
		byte[] name = new byte[32];
		try{
			for(int i=0; i<mode.ssw.length; i++){
				stream.read(name, 0, name.length);
				mode.ssw[i].ip_addr = (new String(name)).trim();
				mode.ssw[i].tx_port = stream.readInt();
				mode.ssw[i].rx_port = stream.readInt();
				mode.ssw[i].status = stream.readInt();
				mode.ssw[i].equip = stream.readInt();
			}
			stream.read(name, 0, name.length);
			mode.rgw.ip_addr = (new String(name)).trim();
			mode.rgw.mgid = stream.readInt();
			mode.rgw.lln = stream.readInt();
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}

	public M3507_MODE getDisCodec(){
		M3507_MODE mode = new M3507_MODE();
		try{
			for(int i=0; i<mode.info.length; i++){
				mode.info[i].equip_flag = stream.readInt();
				mode.info[i].codec_type = stream.readInt();
				mode.info[i].ptime = stream.readInt();
				mode.info[i].vad_cng = stream.readInt();
				mode.info[i].jitter = stream.readInt();
				mode.info[i].jitter_msec = stream.readInt();
				mode.info[i].plc_flag = stream.readInt();
			}

			mode.echo.echo_flag = stream.readInt();
			mode.echo.echo_nlp = stream.readInt();
			mode.echo.echo_cn = stream.readInt();
			mode.echo.echo_tail_len = stream.readInt();
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}

	public M3513_MODE getDisAgLn(){
		M3513_MODE mode = new M3513_MODE();
		byte[] name = null;
		try{
			for(int i=0; i<mode.ln_inf.length; i++){

				mode.ln_inf[i] =  new Ag_ln_data_m();

				mode.ln_inf[i].Ln = stream.readShort();

				mode.ln_inf[i].eqp = (int)stream.readByte();

				name = new byte[8];
				stream.read(name, 0, name.length);
				mode.ln_inf[i].termid = (new String(name)).trim();

				stream.read(name, 0, name.length);
				mode.ln_inf[i].ln_sts = (new String(name)).trim();

				name = new byte[8];
				stream.read(name, 0, name.length);
				mode.ln_inf[i].call_sts = (new String(name)).trim();

				name = new byte[8];
				stream.read(name, 0, name.length);
				mode.ln_inf[i].eph = (new String(name)).trim();

				stream.read(name, 0, name.length);
				mode.ln_inf[i].cxt = (new String(name)).trim();

				name = new byte[24];
				stream.read(name, 0, name.length);
				mode.ln_inf[i].dest_ip = (new String(name)).trim();
				stream.readByte();
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}

	public M3520_MODE getDisRtp(){
		M3520_MODE mode = new M3520_MODE();
		byte[] name = new byte[24];
		try{
			for(int i=0; i<mode.rtp_inf.length; i++){
				mode.rtp_inf[i].flag = stream.readInt();
				mode.rtp_inf[i].eph = stream.readInt();
				mode.rtp_inf[i].phy = stream.readInt();

				stream.read(name, 0, name.length);
				mode.rtp_inf[i].src_ip = (new String(name)).trim();

				stream.read(name, 0, name.length);
				mode.rtp_inf[i].dest_ip = (new String(name)).trim();

				name = new byte[12];
				stream.read(name, 0, name.length);
				mode.rtp_inf[i].codec = (new String(name)).trim();

				name = new byte[8];
				stream.read(name, 0, name.length);
				mode.rtp_inf[i].ptime = (new String(name)).trim();

				stream.read(name, 0, name.length);
				mode.rtp_inf[i].vadcng = (new String(name)).trim();

				name = new byte[4];
				stream.read(name, 0, name.length);
				mode.rtp_inf[i].echo = (new String(name)).trim();
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}


	public M3526_MODE getDisTrcMegaco(){
		M3526_MODE mode = new M3526_MODE();
		byte[] name = new byte[24];
		try{
			for(int i=0; i<mode.trc.length; i++){
				mode.trc[i].mms_flag = stream.readInt();;
				mode.trc[i].rslt = stream.readInt();;
				mode.trc[i].job_index = stream.readInt();;
				mode.trc[i].vop_ln = stream.readInt();;
				mode.trc[i].pid = stream.readInt();;

				stream.read(name, 0, name.length);
				mode.trc[i].ip_addr = (new String(name)).trim();

				mode.trc[i].udp_port = stream.readInt();;
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}


	public M3576_MODE getDisDspTh(){
		M3576_MODE mode = new M3576_MODE();
		try{
			mode.chk_val = stream.readInt();
			for(int i=0; i<mode.VocDB.length; i++){
				mode.VocDB[i].VocAvlCnt = stream.readShort();
				mode.VocDB[i].VocAlocCnt = stream.readShort();
				mode.VocDB[i].LoadFlag = stream.readShort();
				stream.readShort();

				for(int j=0; j<20; j++)
					mode.VocDB[i].VocSts[j] = stream.readInt();
				for(int j=0; j<20; j++)
					mode.VocDB[i].VocUse[j] = stream.readInt();
			}
			mode.max_ts = stream.readInt();
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}

	public M4119_MODE getDisAPing(){
		M4119_MODE mode = new M4119_MODE();
		try{
			mode.onl_flag = stream.readInt();
			mode.prc_flag = stream.readInt();
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}

	public M3580_MODE getDisRtpConf(){
		M3580_MODE mode = new M3580_MODE();
		try{
			mode.dtmf_flag = stream.readInt();
			mode.megaco_dscp = stream.readInt();
			mode.rtp_dscp = stream.readInt();
			mode.fax_mode = stream.readInt();
			mode.pkt_gain = stream.readInt();
			mode.tdm_gain = stream.readInt();
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}

	public M3590_MODE getDisPortInf(){
		M3590_MODE mode = new M3590_MODE();
		byte[] msgByte = new byte[8];
		byte[] methByte = new byte[12];

		try{
			for(int i=0; i<mode.port_inf.length; i++){
				mode.port_inf[i].eqp = (int)stream.readByte();
				mode.port_inf[i].Cid = (int)stream.readByte();
				mode.port_inf[i].Lid = (int)stream.readByte();
				stream.readByte(); //alignment
				mode.port_inf[i].Ln = stream.readShort();
				stream.readShort(); //alignment
				mode.port_inf[i].Lln = stream.readInt();

				stream.read(msgByte, 0, msgByte.length);
				mode.port_inf[i].termid = (new String(msgByte)).trim();

				stream.read(msgByte, 0, msgByte.length);
				mode.port_inf[i].tx_gain = (new String(msgByte)).trim();

				stream.read(msgByte, 0, msgByte.length);
				mode.port_inf[i].rx_gain = (new String(msgByte)).trim();

				stream.read(methByte, 0, methByte.length);
				mode.port_inf[i].meth = (new String(methByte)).trim();
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}

	public M3202_MODE getDisSlnInf(){
		M3202_MODE mode = new M3202_MODE();

		try{
			mode.total = stream.readShort();
			mode.idle = stream.readShort();
			mode.busy = stream.readShort();
			mode.blk = stream.readShort();
			mode.lsblk = stream.readShort();
			mode.llo = stream.readShort();
			mode.piba = stream.readShort();
			mode.test = stream.readShort();
			mode.delt = stream.readShort();
			mode.oos = stream.readShort();

			for(int i=0; i<32; i++)
				for(int j=0; j<32; j++)
					mode.ln_sts[i][j] = stream.readShort();
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}

	public M4117_MODE getActPing(){
		M4117_MODE mode = new M4117_MODE();
		byte[] msgByte = new byte[24];

		try{
			mode.rslt = stream.readInt();

			stream.read(msgByte, 0, msgByte.length);
			mode.ipaddr = (new String(msgByte)).trim();

			mode.dev = stream.readInt();

			msgByte = new byte[72];
			stream.read(msgByte, 0, msgByte.length);
			mode.data = (new String(msgByte)).trim();
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}


	public M3586_MODE getDisCallLmt(){
		M3586_MODE mode = new M3586_MODE();
		try{
			mode.call_cnt = stream.readInt();
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}

	public M3583_MODE getDisProtocol(){
		M3583_MODE mode = new M3583_MODE();
		try{
			mode.re_cnt = stream.readInt();
			mode.re_time = stream.readInt();
			mode.t1_mode = stream.readInt();
			mode.t1_time = stream.readInt();
			mode.interval = stream.readInt();
			mode.thres = stream.readInt();
			mode.check_time = stream.readInt();
			mode.swt_mode = stream.readInt();
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}

	public String getErrorReason()
	{
		byte[] err = new byte[ERROR_LENGTH];
		try
		{
			stream.read(err, 0, err.length);
			return (new String(err)).trim();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
		finally
		{
			try
			{
				stream.close();
			}catch(Exception e1){}
		}
	}

	public int close()
	{
		try
		{
			stream.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return -1;
		}
		return 0;
	}
}

